package com.djl.seckilldemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.djl.seckilldemo.exception.GlobalException;
import com.djl.seckilldemo.mapper.OrderMapper;
import com.djl.seckilldemo.pojo.Order;
import com.djl.seckilldemo.pojo.SeckillGoods;
import com.djl.seckilldemo.pojo.SeckillOrder;
import com.djl.seckilldemo.pojo.User;
import com.djl.seckilldemo.service.IGoodsService;
import com.djl.seckilldemo.service.IOrderService;
import com.djl.seckilldemo.service.ISeckillGoodsService;
import com.djl.seckilldemo.service.ISeckillOrderService;
import com.djl.seckilldemo.util.MD5Util;
import com.djl.seckilldemo.util.UUIDUtil;
import com.djl.seckilldemo.vo.GoodsVo;
import com.djl.seckilldemo.vo.OrderDetailVo;
import com.djl.seckilldemo.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-10-22
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private ISeckillGoodsService seckillGoodsService;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ISeckillOrderService seckillOrderService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private RedisTemplate redisTemplate;
    //事务注解
    @Transactional
    @Override
    public Order seckill(User user, GoodsVo goods) {
        //秒杀商品减库存
        SeckillGoods seckillGoods = seckillGoodsService.getOne(new QueryWrapper<SeckillGoods>().eq("goods_id", goods.getId()));
        boolean result = seckillGoodsService.update(new UpdateWrapper<SeckillGoods>().setSql("seckill_count=seckill_count-1")
                .eq("goods_id", goods.getId())
                .gt("seckill_count", 0));
        if (seckillGoods.getSeckillCount()<1){
            //判断是否还有库存
            redisTemplate.opsForValue().set("isStockEmpty:"+seckillGoods.getGoodsId(),true);
            return null;
        }
        //生成订单
        Order order = new Order();
        order.setId(seckillGoods.getGoodsId());
        order.setUserId(user.getId());
        order.setDeliveryAddrId(0L);
        order.setGoodsId(goods.getId());
        order.setGoodsName(goods.getGoodsName());
        order.setGoodsCount(1);
        order.setGoodsPrice(seckillGoods.getSeckillPrice());
        order.setOrderChannel(1);
        order.setStatus(0);
        order.setCreateDate(new Date());
        System.out.println(seckillGoodsService.getById(1));
        System.out.println(order.toString());
        System.out.println("------------------");
        orderMapper.insert(order);
        //生成秒杀订单
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setOrderId(order.getId());
        seckillOrder.setGoodsId(seckillGoods.getGoodsId());
        seckillOrder.setUserId(user.getId());
        seckillOrderService.save(seckillOrder);
        redisTemplate.opsForValue().set("order:"+user.getId()+":"+goods.getId(),seckillOrder);
        return order;
    }

    /**
     * 功能描述：订单详情
     * @param orderId
     * @return
     */
    @Override
    public OrderDetailVo detail(Long orderId) {
        if(orderId==null){
            throw new GlobalException(RespBeanEnum.ORDER_NO_EXIST);
        }
        Order order=orderMapper.selectById(orderId);
        GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(order.getGoodsId());
        OrderDetailVo detail=new OrderDetailVo();
        detail.setOrder(order);
        detail.setGoodsVo(goodsVo);


        return detail;
    }

    @Override
    public String createPath(User user, Long goodsId) {
        String s = MD5Util.md5(UUIDUtil.uuid() + "123456");
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("seckillPath:"+user.getId()+":"+goodsId,s,60, TimeUnit.SECONDS);

        return s;
    }

    /**
     * 校验秒杀地址
     * @param user
     * @param goodsId
     * @param path
     * @return
     */
    @Override
    public Boolean checkPath(User user, Long goodsId, String path) {
        if (user==null|| StringUtils.isEmpty(path)){
            return false;
        }
        String redisPath =(String) redisTemplate.opsForValue().get("seckillPath:" + user.getId() + ":" + goodsId);
        return path.equals(redisPath);
    }
}
